
export default class DateUtils {
  /**
   * @param y 年
   * @param m 月
   */
  static getDateSource = (y?: number, m?: number) => {
    const years: string[] = [];
    const months: string[] = [];
    const days: string[] = [];
    const date = new Date();
    const len = date.getFullYear() + 30;

    // 是否闰年
    function isRunYear(fullYear) {
      return fullYear % 4 == 0 && (fullYear % 100 != 0 || fullYear % 400 == 0);
    }

    for (let i = 1940; i <= len; i++) {
      years.push(i + "");
    }

    for (let i = 1; i <= 12; i++) {
      const value = i < 10 ? "0" + i : i + "";
      months.push(value);
    }

    let numberOfDays = [1, 3, 5, 7, 8, 10, 12].includes(m as number) ? 31 : 30;

    if (Number(m)=== 2) {
      if (y && isRunYear(y)) {
        numberOfDays = 29;
      } else {
        numberOfDays = 28;
      }
    }

    for (let i = 1; i <= numberOfDays; i++) {
      const value = i < 10 ? "0" + i : i + "";
      days.push(value);
    }

    return {
      years,
      months,
      days,
    };
  };

  static getToDayIndexs(): number[] {
    const date = new Date();
    const y = date.getFullYear();
    const m = date.getMonth() + 1;
    const d = date.getDate();
    const { years, months, days } = DateUtils.getDateSource(y, m);
    const mText = m < 10 ? "0" + m : m + '';
    const dText = d < 10 ? "0" + d : d + '';
    const yIndex = years.findIndex((item) => item === String(y));
    const mIndex = months.findIndex((item)=> item === mText);
    const dIndex = days.findIndex((item) => item === dText);
    return [yIndex, mIndex, dIndex];
  }

  static dateToNumerical(date: string) {
    const [y = 1940, m = 1, d = 1] = (date as string)?.split("-") || [];
    return [Number(y), Number(m), Number(d)];
  }

  /**
   *
   * @param selectDate 当前选择的日期
   * @param targetDate 对比的目标日期
   * @param mode 对比方式 大于或者小于 gt大于  lt 小于
   */
  static compare(selectDate, targetDate: string, mode: 'gt' | 'lt'): boolean {
    const [y, m, d] = DateUtils.dateToNumerical(selectDate);
    const [tY, tM, tD] = DateUtils.dateToNumerical(targetDate);
    if (mode === 'gt') {
        if (y > tY) {
            return true
        }
        if (y === tY && m > tM) {
            return true
        }
        if (y === tY && m === tM && d > tD) {
            return true;
        }
    } else {

        if (y < tY) {
            return true
        }
        if (y === tY && m < tM) {
            return true
        }
        if (y === tY && m === tM && d < tD) {
            return true;
        }
    }

    return false;
  }

  static findIndexs(value: string) {
    const [y, m, d] = DateUtils.dateToNumerical(value);
    const {years, months, days} = DateUtils.getDateSource(Number(y), Number(m));
    const mText = Number(m) < 10 ? "0" + m : m + '';
    const dText =Number(d) < 10 ? "0" + Number(d) : d + '';
    const yIndex = years.findIndex((item) => item === String(y));
    const mIndex = months.findIndex((item)=> item === mText);
    const dIndex = days.findIndex((item) => item === dText);
    return [yIndex, mIndex, dIndex];
  }

}
